package com.sgb.zhaocai.viewmodel;

import android.util.Log;
import android.view.View;

import androidx.lifecycle.ViewModel;
import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.MapLongAndLatEntity;
import com.sgb.zhaocai.entity.ProjectIssueBean;
import com.sgb.zhaocai.entity.PublishListBean;
import com.sgb.zhaocai.view.demand.activity.NewAddProjectActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import java.util.List;

/**
 * Time:2021/9/18
 * AUTHOR:F_Q
 * Description:
 */
public class NewAddProjectModel extends ViewModel {
    /**
     * 获取经纬度信息
     * @param activity
     * @param provinceNo
     * @param cityNo
     * @param areaNo
     */
    public void getLocationLL(NewAddProjectActivity activity, String provinceNo, String cityNo, String areaNo) {
        String areaCode = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.AppProvinceCode, "");
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getLocationLL(areaCode,provinceNo,cityNo,areaNo).enqueue(new ICallBack<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onSuccess(BaseEntity<MapLongAndLatEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.updateAddress(data.getData());
                }else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * 地址信息
     * @param activity
     */
    public void getAddressModel(NewAddProjectActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    activity.setAddressList(data.getData());
                }
            }
        });
    }

    /**
     * 工程类型
     *
     * @param activity
     * @param view
     */
    public void getProjectTypeList(NewAddProjectActivity activity, View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getProjectTypeList().enqueue(new ICallBack<BaseEntity<List<PublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setProjectTypeList(data.getData(), view);
                    } else {
                        MToast.showToast(activity, "当前单位无部门信息");
                    }
                }
            }
        });
    }

    /**
     * 项目立项-新增
     * @param activity
     * @param bean
     */
    public void saveOrUpdateProject(NewAddProjectActivity activity, ProjectIssueBean bean) {
        Log.e("tag项目发布",bean.toString() );
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().saveOrUpdateProject(bean).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.setSaveOrUpdateProject(bean.getAuditStatus());
                }else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

}
